package pers.xiaoliang.concurrency.example.singleton;

import lombok.extern.slf4j.Slf4j;
import pers.xiaoliang.concurrency.annotation.NotThreadSafe;

/**
* @description: 单例发布对象(懒汉模式- 线程不安全(cpu指令重排导致) 双重判断) - 又名双重同步锁单例模式
* @author: xiaoliang.liu
* @date: 2018/7/28 13:36
* @version v1.0
*/
@Slf4j
@NotThreadSafe
public class SingletonExample4 {

    private SingletonExample4() {}


    /** 单例对象 */
    private static SingletonExample4 instance = null;

    /** 静态工厂方法 */
    public static SingletonExample4 getInstance() {
        if (instance == null) {
            synchronized (SingletonExample4.class){
                if (instance == null) {
                    instance = new SingletonExample4();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {

    }
}
